import 'package:flutter/material.dart';

void main() => runApp(TextApp());

///应用的根布局
class TextApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    ///构建Materia Desin 风格的应用程序
    return MaterialApp(
      debugShowCheckedModeBanner: false,

      ///默认的首页面
      home: NavaHomePage3(),
    );
  }
}
///使用 Scaffold ，在其提供的 bottomNavigationBar 的属性中 传入 BottomNavigationBar，
///然后 items 设置BottomNavigationBarItem 类型的数组，再按需设定其它一些属性，
///提供的可配置属性也基本能满足常规 app 的需求。
///通过 BottomNavigationBar 组件来实现底部导航栏还是很方便的
class NavaHomePage3 extends StatefulWidget {
  const NavaHomePage3({super.key});

  @override
  _NavaHomePage3State createState() => _NavaHomePage3State();
}

class _NavaHomePage3State extends State<NavaHomePage3> {
  int _selectedIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: buidPage(),
        bottomNavigationBar: BottomAppBar(
          color: Color(0xfffafafa),
          shape: const CircularNotchedRectangle(),
          child: Row(
            children: <Widget>[
              IconButton(
                icon: Icon(
                  Icons.home,
                  color: _selectedIndex==0?Colors.blue:Colors.grey,
                ),
                onPressed: () => _onItemTapped(0),
              ),
              IconButton(
                icon: Icon(
                  Icons.business,
                  color: _selectedIndex==1?Colors.blue:Colors.grey,
                ),
                onPressed: () => _onItemTapped(1),
              ),
              SizedBox(), // 增加一些间隔
              IconButton(
                icon: Icon(
                  Icons.school,
                  color: _selectedIndex==2?Colors.blue:Colors.grey,
                ),
                onPressed: () => _onItemTapped(2),
              ),
              IconButton(
                icon: Icon(
                  Icons.search,
                  color: _selectedIndex==3?Colors.blue:Colors.grey,
                ),
                onPressed: () => _onItemTapped(3),
              ),
            ],
            mainAxisAlignment: MainAxisAlignment.spaceAround,
          ),
        ),
    );
  }

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  buidPage() {
    if (_selectedIndex == 0) {
      return Text("页面一");
    }

    if (_selectedIndex == 2) {
      return Text("页面二");
    }

    if (_selectedIndex == 3) {
      return Text("页面三");
    }

    return Text("页面四");
  }
}
